package com.lw.common.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import com.alibaba.fastjson.JSONObject;

import javax.crypto.SecretKey;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
public class Utils {

    /**
     *
     * @param paramObj
     * @return  json 转map
     */
    public static HashMap<String, String> json2Map(JSONObject paramObj ) {
        HashMap<String, String> params = new HashMap<>();
        Iterator<String> iterator = paramObj.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            String value = paramObj.getString(key);
            params.put(key, value);
        }
        return params;
    }

    public static String getYyyyMmDd(Calendar instance) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(instance.getTime());
        format += "000000";
        return format;
    }

    private static String key = "china_qx@58140";

    //加密
    public static String encryptHex(String data) {
        SecretKey secretKey = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(), key.getBytes());
        DES des = SecureUtil.des(secretKey.getEncoded());
       return des.encryptHex(data);
    }
    //解密
    public static String decryptHex(String data) {
        SecretKey secretKey = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue(), key.getBytes());
        DES des = SecureUtil.des(secretKey.getEncoded());
        return des.decryptStr(data);
    }

}
